package com.baseDemo.web.service.docinfo.impl;

import java.io.File;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baseDemo.web.dao.docinfo.DocInfoEntityDao;
import com.baseDemo.web.entity.DocInfoEntity;
import com.baseDemo.web.server.SystemServer;
import com.baseDemo.web.service.docinfo.DocInfoService;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;

@Service
public class DocInfoServiceImpl extends BaseServiceSupport<DocInfoEntity>
		implements DocInfoService {

	@Resource
	DocInfoEntityDao docInfoEntityDao;

	@Override
	public String upload(DocInfoEntity docInfoEntity, HttpServletRequest request) {
		try {
			CommonsMultipartFile multipartFile = docInfoEntity.getMultipartFile();
			docInfoEntity.setName(multipartFile.getOriginalFilename());
			docInfoEntity.setType(multipartFile.getContentType());
			docInfoEntity.setFileSize(multipartFile.getSize());
			docInfoEntity.setThumb(SystemServer.getInstance().get("OFFICEIMAGE")
					+ multipartFile.getContentType() + ".png");
			String path = request.getSession().getServletContext()
					.getRealPath("upload");
			String newName = Calendar.getInstance().getTimeInMillis()+multipartFile.getOriginalFilename();
			File targetFile = new File(path, newName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			multipartFile.transferTo(targetFile);
			System.out.println(path);
			docInfoEntity.setUrl("upload"+File.separator+newName);
			return "{'success':'"+docInfoEntityDao.save(docInfoEntity)+"'}";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "{'success':'0'}";
		// return "{'success':'"+docInfoEntityDao.save(docInfoEntity)+"'}";
	}

	@Override
	public List<DocInfoEntity> findDocInfoByCatalog(String catalogId) {
		return docInfoEntityDao.findByCatalogId(catalogId);
	}

}
